package com.tanhua.manage.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanhua.manage.mapper.UserMapper;
import com.tanhua.manage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FreezesService {

    @Autowired
    private UserMapper userMapper;

    //冻结
    public Boolean userFreeze(Long userId, Integer freezingTime, Integer freezingRange, String reasonsForFreezing, String frozenRemarks) {
        //利用lambda表达式
        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper
                .eq(User::getId,userId) //修改的userId
                .set(User::getUpdated,new Date()) //更新修改时间
                .set(User::getFrozenTime,freezingTime)//添加冻结状态种种
                .set(User::getFrozenRange,freezingRange)
                .set(User::getFrozenReason,reasonsForFreezing)
                .set(User::getFrozenRemark,frozenRemarks)
                .set(User::getThawedReason,null); //置空解冻状态

        int result = userMapper.update(null,lambdaUpdateWrapper);

        return result == 1;
    }

    //解冻
    public Boolean userUnfreeze(Long userId, String reasonsForThawing) {

        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper
                .eq(User::getId,userId) //修改的userId
                .set(User::getUpdated,new Date()) //更新修改时间
                .set(User::getFrozenTime,0)//置空冻结状态种种
                .set(User::getFrozenRange,0)
                .set(User::getFrozenReason,null)
                .set(User::getFrozenRemark,null)
                .set(User::getThawedReason,reasonsForThawing); //添加解冻原因

        int result = userMapper.update(null,lambdaUpdateWrapper);

        return result == 1;
    }
}
